#ifndef DEVICE_H
#define DEVICE_H

#include <QObject>
#include <QJsonObject>
#include <QString>
#include <QMap>
#include "../network/websocketclient.h"

class Device : public QObject
{
    Q_OBJECT

public:
    explicit Device(const QString &ipAddress, const QString &deviceId, QObject *parent = nullptr);
    ~Device();

    // 设备基本信息
    QString getIpAddress() const { return m_ipAddress; }
    QString getDeviceId() const { return m_deviceId; }
    
    // 设备连接相关方法
    void connectToDevice();
    void disconnectFromDevice();
    bool isConnected() const;
    
    // 登录相关方法
    void requestChallenge(const QString &username);
    void login(const QString &username, const QString &challenge, 
               const QString &credential, bool rememberMe = false);
               
    // 网络配置相关方法
    void getNetworkConfig();
    void setNetworkConfig(const QString &interfaceName, bool enable, const QString &mode, 
                         const QString &ipAddress = QString(), const QString &netmask = QString(), 
                         const QString &gateway = QString());
    
    // 固件升级相关方法
    void uploadFirmware(int chunkIndex, int totalChunks, const QByteArray &chunkData, 
                       const QString &fileName = QString(), qint64 fileSize = 0, 
                       const QString &version = QString(), const QString &md5 = QString());
    void startFirmwareUpgrade(const QString &version, bool force = false);
    void getFirmwareUpgradeStatus();

    // 访问器方法
    QString getAccessToken() const { return m_accessToken; }
    QString getRefreshToken() const { return m_refreshToken; }
    QJsonObject getUserInfo() const { return m_userInfo; }
    bool isLoggedIn() const { return m_isLoggedIn; }
    QString getCurrentChallenge() const { return m_currentChallenge; }

signals:
    // 连接状态变化信号
    void connectionStateChanged(const QString &ipAddress, bool connected);
    // 错误信号
    void errorOccurred(const QString &ipAddress, const QString &errorMessage);
    // 登录相关信号
    void challengeReceived(const QString &ipAddress, const QString &challenge, int expiresIn);
    void loginSuccess(const QString &ipAddress, const QString &accessToken, const QString &refreshToken, 
                     const QJsonObject &userInfo);
    void loginFailed(const QString &ipAddress, int errorCode, const QString &errorMessage);
    // 网络配置相关信号
    void networkConfigReceived(const QString &ipAddress, const QJsonObject &config);
    void networkConfigSetResult(const QString &ipAddress, bool success, const QString &message);
    
    // 固件升级相关信号
    void firmwareUploadProgress(const QString &ipAddress, int progress);
    void firmwareUploadComplete(const QString &ipAddress);
    void firmwareUpgradeStarted(const QString &ipAddress, const QString &upgradeId, const QString &status, int estimatedDuration);
    void firmwareUpgradeStatusReceived(const QString &ipAddress, const QString &status, int progress, const QString &message);
    void firmwareUpgradeError(const QString &ipAddress, const QString &errorMessage);

private slots:
    // WebSocket连接和登录相关槽函数
    void onWebSocketConnectionStateChanged(bool connected);
    void onWebSocketError(const QString &errorMessage);
    void onChallengeReceived(const QString &challenge, int expiresIn);
    void onLoginSuccess(const QString &accessToken, const QString &refreshToken, const QJsonObject &userInfo);
    void onLoginFailed(int errorCode, const QString &errorMessage);
    // 网络配置相关槽函数
    void onNetworkConfigReceived(const QJsonObject &config);
    void onNetworkConfigSetResult(bool success, const QString &message);
    
    // 固件升级相关槽函数
    void onFirmwareUploadProgress(int progress);
    void onFirmwareUploadComplete();
    void onFirmwareUpgradeStarted(const QString &upgradeId, const QString &status, int estimatedDuration);
    void onFirmwareUpgradeStatusReceived(const QString &status, int progress, const QString &message);
    void onFirmwareUpgradeError(const QString &errorMessage);

private:
    QString m_ipAddress;          // 设备IP地址（唯一标识）
    QString m_deviceId;           // 客户端设备ID
    WebSocketClient *m_webSocketClient;
    
    // 登录相关变量
    QString m_currentChallenge;   // 当前挑战码
    QString m_accessToken;        // 访问令牌
    QString m_refreshToken;       // 刷新令牌
    bool m_isLoggedIn;            // 是否已登录
    QJsonObject m_userInfo;       // 用户信息
};

#endif // DEVICE_H